-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
39 lines (33 loc) · 996 Bytes
/
Solution.py
File metadata and controls
39 lines (33 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Stack:
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
print(f"{value} pushed into the stack.")
def pop(self):
if not self.stack:
print("Stack underflow!")
else:
print(f"{self.stack.pop()} popped from the stack.")
def peek(self):
if not self.stack:
print("Stack is empty.")
else:
print(f"Top element is: {self.stack[-1]}")
if __name__ == "__main__":
stack = Stack()
while True:
print("\n1. Push\n2. Pop\n3. Peek\n4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
value = int(input("Enter value to push: "))
stack.push(value)
elif choice == 2:
stack.pop()
elif choice == 3:
stack.peek()
elif choice == 4:
print("Exiting...")
break
else:
print("Invalid choice!")